// Funkcija koja puni oderedjeni select element
// Argumenti:
//      o_select:   objekt kojega punimo
//      a_array:    niz iz kojega se puni
function fill_select(o_select, url_array)
{
    var i_separator, i, s_opt_text, s_opt_value;
	
    // Postavimo duljinu niza na 0
    o_select.options.length = 0;
    for(i=0; i < url_array.length; i++)
	{
        s_opt_text = url_array[i];
        s_opt_value = i;
        
        o_select.options[i] = new Option(s_opt_text, s_opt_value);      // Postavimo vrijednosti
    }
} 



function XmlToString(xmlNode)
{
  try {
    // Gecko-based browsers, Safari, Opera.
    return (new XMLSerializer()).serializeToString(xmlNode);
  }
  catch (e) {
    try {
      // Internet Explorer.
      return xmlNode.xml;
    }
    catch (e)
    {//Strange Browser ??
     alert('Xmlserializer not supported');
    }
  }
  return false;
}

function createXMLFromString(text)
{
	var xmlDoc = null;

  try //Internet Explorer
  {
	xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async="false";
	xmlDoc.loadXML(text);
  }
	catch(e)
  {
		try //Firefox, Mozilla, Opera, etc.
		{

			xmlDoc= (new DOMParser()).parseFromString(text,"text/xml");
			
		}
		catch(e) 
		{
			alert(e.message)
		}
  }
	
	return xmlDoc;
}

function removeAllChildNodes(node) 
{
    if (node && node.parentNode && node.parentNode.replaceChild && node.cloneNode) 
    {
        node.parentNode.replaceChild(node.cloneNode(false), node);
    }
} 
function removeAllChildNodes2(node) 
{
    if (node && node.hasChildNodes && node.removeChild) 
    {
        while (node.hasChildNodes()) 
        {
            node.removeChild(node.firstChild);
        }
    }
} 


//bijelizeko: helper hacks

//carefull: The problem is that the Firefox DOM does not ignore line breaks and whitespaces while IE does.
function getFirstChildElement(elm)
{
    if (!elm.childNodes)
    {
    return;
    }
    
    var children = elm.childNodes.length;
    
    for ( var i = 0; i <= children; i++ )
    {
        if ( elm.childNodes[i].nodeType == 1 )
        {
            return elm.childNodes[i];
        }
    }

    return;
}

//carefull: The problem is that the Firefox DOM does not ignore line breaks and whitespaces while IE does.
function getLastChildElement(elm)
{
    if (!elm.childNodes)
    {
    return;
    }
    
    var children = elm.childNodes.length;
    
    for ( var i = children-1; i > 0; i-- )
    {
        if ( elm.childNodes[i].nodeType == 1 )
        {
            return elm.childNodes[i];
        }
    }

    return;
}

function createTable(msgs, srcLang, dstLang)
{  

    var container = document.getElementById('results_translate_table');
  
    if (container.firstChild && container && container.parentNode && container.parentNode.replaceChild && container.cloneNode) 
    {  
        //container.parentNode.replaceChild(container.cloneNode(false), container);
        while (container.hasChildNodes()) 
        {
            container.removeChild(container.firstChild);
        }
    }
  
    var table = document.createElement("TABLE");
    
    table.id = "msgstable";
    table.className = "msgsTable";
    
    
    var tablebody = document.createElement("TBODY");
    tablebody.id = "msgsTableBody";
    
    table.appendChild(tablebody);

    container.appendChild(table);
    
    for (key in tpairs)
    {
    
        createTableRow(key, tpairs[key].src, tpairs[key].dest);
    }
    
//    for(key in  msgs)
//    {
//        translateMessage(key, msgs[key], srcLang, dstLang);
//    }
	
}


function createTableRow(key, message, translation)
{   
    //var container = document.firstChild.firstChild;
    //var container = document.getElementById('results_translate_table');
    
    //carefull: The problem is that the Firefox DOM does not ignore line breaks and whitespaces while IE does.
    //var tablebody = container.firstChild.lastChild;
    
    //Fix Bug in Mozilla: The problem is that the Firefox DOM does not ignore line breaks and whitespaces while IE does.
    //var table = getFirstChildElement(container);
    //var tablebody = getLastChildElement(table);
    
    var tablebody = document.getElementById("msgsTableBody");
    
    var rowSrc = document.createElement("TR");
    rowSrc.id = key + "_row_src";
    rowSrc.className = getRowClassString('row_src');
    
    var tableDataSrc = document.createElement("TD");
    tableDataSrc.id = key + "_data_src";
    tableDataSrc.className = getRowClassString('data_src');
    tableDataSrc.appendChild(document.createTextNode(message));
    
    var tableDataMenuSrc = document.createElement("TD");
    tableDataMenuSrc.id = key + "_menu_src";
    tableDataMenuSrc.className = getRowClassString('data_menu');
    //var menu_src = "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"editMsgSrc('" +key+ "');\">Edit</td><td class=\"menuItem\" onclick=\"hideRow('" + key + "');\">Hide</td></tr></tbody></table>" ;
    tableDataMenuSrc.innerHTML = getMenuString('src', key);
    
    var rowDest = document.createElement("TR");
    rowDest.id = key + "_row_dest";
    rowDest.className = getRowClassString('row_dest');
    
    
    var tableDataDest = document.createElement("TD");
    tableDataDest.id = key + "_data_dest";
    tableDataDest.className = getRowClassString('data_dest');
    tableDataDest.appendChild(document.createTextNode(translation));
    
    var tableDataMenuDest = document.createElement("TD");
    tableDataMenuDest.id = key + "_menu_dest";
    tableDataMenuDest.className = getRowClassString('data_menu');
    //var menu_dest = "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"editMsgDest('" +key+ "');\">Edit</td></tr></tbody></table>" ;
    tableDataMenuDest.innerHTML = getMenuString('dest', key);
    
        
    rowSrc.appendChild(tableDataSrc);
    rowSrc.appendChild(tableDataMenuSrc);

    rowDest.appendChild(tableDataDest);
    rowDest.appendChild(tableDataMenuDest);
    
    
    tablebody.appendChild(rowSrc);
    tablebody.appendChild(rowDest);
    
    
    
}


function createTableString(translationPairs)
{  
    //_gel()
    var container = document.getElementById('results_translate_table');
  
  
    var strHTML = "<table id=\"mbtable\" class=\"msgsTable\"><tbody id = \"msgsTableBody\" >";
    
    for(key in translationPairs)
    {
        
        strHTML = strHTML + createTableRowString(key, translationPairs[key].src, translationPairs[key].dest);
    
    }
   
    strHTML+="</tbody></table>";
    
    
    container.innerHTML = strHTML;
  

}	

function createTableRowString(key, src, dest)
{
	//"<td><table><tbody><tr><td><a href=''>Edit</a></td><td><a href=''>Show</a></td></tr></tbody></table></td>"+
	//"<td align='middle' nowrap id='" + key + "_edit_src'><a>Edit</a>|<a>Del</a></td>"+
    var strHTML = "<tr id=\"" + key + "_row_src\" class = \"" + getRowClassString('row_src') + "\" >" +
                    "<td id=\"" + key + "_data_src\" class = \""+ getRowClassString('data_src')+ "\" >" + src + "</td>" +
					"<td id=\"" + key + "_menu_src\" class = \"" + getRowClassString('data_menu')+"\" >" + "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"editMsgSrc('" +key+ "');\">Edit</td><td class=\"menuItem\" onclick=\"hideRow('" + key + "');\">Hide</td></tr></tbody></table>" + "</td>" +				 
				  "</tr>"+
                  "<tr id=\"" + key + "_row_dest\" class = \"" + getRowClassString('row_dest') + "\" >" +
                    "<td id=\"" + key + "_data_dest\" class = \""+ getRowClassString('data_dest')+ "\" >" + dest + "</td>" +
					"<td id=\"" + key + "_menu_dest\" class = \"" + getRowClassString('data_menu')+"\" >" + "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"editMsgDest('" +key+ "');\">Edit</td></tr></tbody></table>" + "</td>" +				 
				  "</tr>";
								
							
	return strHTML;

}

function getRowClassString(type) 
{
	switch(type)
	{
		case 'row_src': return "msgsRowShowSrc"
		break;
		
		case 'data_src': return "msgsTableDataSrc"
		break;
		
		case 'row_dest': return "msgsRowShowDest"
		break;
		
		case 'data_dest': return "msgsTableDataDest"
		break;
		
		case 'dest_hide': return "msgsRowHide"
		break;
		
		case 'data_menu': return "msgsTableDataMenu"
		break;
		
		case 'menu': return "menu"
		break;
		
		case 'menuItem': return "menuItem"
		break;
	
	}
	
	return "class = 'msgsRowShow'";
}

function getMenuString(type, key)
{
    switch(type)
	{
		case 'show_row_src': return "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"editMsgSrc('" +key+ "');\">Edit</td><td class=\"menuItem\" onclick=\"showRow('" + key + "');\">Show</td></tr></tbody></table>"
		break;
	
	    case 'src': return "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"editMsgSrc('" +key+ "');\">Edit</td><td class=\"menuItem\" onclick=\"hideRow('" + key + "');\">Hide</td></tr></tbody></table>"
		break;
	
		case 'dest': return "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"editMsgDest('" +key+ "');\">Edit</td></tr></tbody></table>"
		break;
		
		case 'edit_mode_src': return "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"saveMsgSrc('" +key+ "');\">Save</td><td class=\"menuItem\" onclick=\"cancelMsgSrc('" + key + "');\">Cancel</td></tr></tbody></table>"
		break;
		
		case 'edit_mode_dest': return "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"saveMsgDest('" +key+ "');\">Save</td><td class=\"menuItem\" onclick=\"cancelMsgDest('" + key + "');\">Cancel</td></tr></tbody></table>"
		break;
		
	}
	
	return "Erorr";

}

function hideRow(key)
{
    var id_row = key + "_row_dest";
    var id_menu = key + "_menu_src";
    
    var str_menu_src = "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"editMsgSrc('" +key+ "');\">Edit</td><td class=\"menuItem\" onclick=\"showRow('" + key + "');\">Show</td></tr></tbody></table>";
    
    //_gel
    document.getElementById(id_row).className = "msgsRowHide";
    
    document.getElementById(id_menu).innerHTML = str_menu_src;
    
    //document.getElementById(id_row).setAttribute('className','msgsRowHide');

    
    
}


function showRow(key)
{
    var id_row = key + "_row_dest";
    var id_menu = key + "_menu_src";
    
    var str_menu_src = "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"editMsgSrc('" +key+ "');\">Edit</td><td class=\"menuItem\" onclick=\"hideRow('" + key + "');\">Hide</td></tr></tbody></table>";
    
    document.getElementById(id_row).className = "msgsRowShowDest";
    
    document.getElementById(id_menu).innerHTML = str_menu_src;

}

var edit_mode = false;
var tpairs = {}; 


//FIXME: make general functions: editMsg, saveMsg, cancelMsg
function editMsgSrc(key)
{
    if(edit_mode) return;
    
    edit_mode = true;
    
    var id_row = key + "_row_dest";
    var id_data = key + "_data_src";
    var id_menu = key + "_menu_src";
    
    document.getElementById(id_data).className ="msgsTableDataEditSrc";
    document.getElementById(id_row).className = "msgsRowShowDest";// if hidden show row dest

    var str_menu_src = "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"saveMsgSrc('" +key+ "');\">Save</td><td class=\"menuItem\" onclick=\"cancelMsgSrc('" + key + "');\">Cancel</td></tr></tbody></table>";
    document.getElementById(id_menu).innerHTML = str_menu_src;

    if(tpairs[key])
    {
        //TODO
        //var msg_value = document.getElementById(id_data).firstChild.nodeValue;
        var msg_value = tpairs[key].src;
        
        var strHTML = "<input id=\"" + key + "_txt_src\" value=\""+ msg_value +"\" >";
        //var strHTML = "<textarea id=\"" + key + "_txt_src\" name=\"" + key + "_txt_src\" cols=\"10\" rows=\"2\">" + msg_value + "</textarea>";

        document.getElementById(id_data).innerHTML = strHTML;
        
        id_txt_src = key + "_txt_src";
        document.getElementById(id_txt_src).focus();
    }
   
}


function saveMsgSrc(key)
{
    edit_mode = false;
    
    var id_data = key + "_data_src";
    var id_menu = key + "_menu_src";
    var id_txt_src = key + "_txt_src";
    
    var msg_src = document.getElementById(id_txt_src).value;
    
    if(tpairs[key])
    {
        tpairs[key].src = msg_src;
   
    }
    
    document.getElementById(id_data).className = "msgsTableDataSrc";
    document.getElementById(id_data).innerHTML = msg_src;
    
     var str_menu_src = "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"editMsgSrc('" +key+ "');\">Edit</td><td class=\"menuItem\" onclick=\"hideRow('" + key + "');\">Hide</td></tr></tbody></table>";
     document.getElementById(id_menu).innerHTML = str_menu_src;
    

}

function cancelMsgSrc(key)
{
    edit_mode = false;
    
    var id_data = key + "_data_src";
    var id_menu = key + "_menu_src";
    var id_txt_src = key + "_txt_src";
    
    var msg_src = "Erorr";
    
    if(tpairs[key])
    {        
         msg_src = tpairs[key].src;
   
    }
    
    document.getElementById(id_data).className = "msgsTableDataSrc";
    document.getElementById(id_data).innerHTML = msg_src;
    
     var str_menu_src = "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"editMsgSrc('" +key+ "');\">Edit</td><td class=\"menuItem\" onclick=\"hideRow('" + key + "');\">Hide</td></tr></tbody></table>";
     document.getElementById(id_menu).innerHTML = str_menu_src;

}

function editMsgDest(key)
{
    if(edit_mode) return;
    
    edit_mode = true;
    
    var id_data = key + "_data_dest";
    var id_row = key + "_row_dest";
    var id_menu = key + "_menu_dest";
    
    document.getElementById(id_data).className ="msgsTableDataEditDest";
    

    var str_menu_src = "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"saveMsgDest('" +key+ "');\">Save</td><td class=\"menuItem\" onclick=\"cancelMsgDest('" + key + "');\">Cancel</td></tr></tbody></table>";
    document.getElementById(id_menu).innerHTML = str_menu_src;
    
    
    if(tpairs[key])
    {
  
        //var msg_value = document.getElementById(id_data).firstChild.nodeValue;
        var msg_value = tpairs[key].dest;
        
        var strHTML = "<input id=\"" + key + "_txt_src\" value=\""+ msg_value +"\" >";
        //var strHTML = "<textarea id=\"" + key + "_txt_src\" name=\"" + key + "_txt_src\" cols=\"10\" rows=\"2\">" + msg_value + "</textarea>";

        document.getElementById(id_data).innerHTML = strHTML;
        
        id_txt_src = key + "_txt_src";
        document.getElementById(id_txt_src).focus();
        
    }
   
}

function saveMsgDest(key)
{
    edit_mode = false;
    
    var id_data = key + "_data_dest";
    var id_menu = key + "_menu_dest";
    var id_txt_dest = key + "_txt_dest";
    
    var msg_dest = document.getElementById(id_txt_src).value;
    
    if(tpairs[key])
    {
        tpairs[key].dest = msg_dest;
   
    }
    
    document.getElementById(id_data).className = "msgsTableDataDest";
    document.getElementById(id_data).innerHTML = msg_dest;
    
     var str_menu_src = "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"editMsgDest('" +key+ "');\">Edit</td></tr></tbody></table>";
     document.getElementById(id_menu).innerHTML = str_menu_src;
    

}

function cancelMsgDest(key)
{
    edit_mode = false;
    
    var id_data = key + "_data_dest";
    var id_menu = key + "_menu_dest";
    var id_txt_src = key + "_txt_dest";
    
    var msg_src = "Error";
    
    if(tpairs[key])
    {        
         msg_src = tpairs[key].dest;
         
   
    }
    
    document.getElementById(id_data).className = "msgsTableDataDest";
    document.getElementById(id_data).innerHTML = msg_src;
    
    var str_menu_src = "<table class=\"menu\"><tbody><tr><td class=\"menuItem\" onclick=\"editMsgDest('" +key+ "');\">Edit</td></tr></tbody></table>";
    document.getElementById(id_menu).innerHTML = str_menu_src;

}